package com.sunnada.edu.business.group.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.business.group.dao.GroupDao;
import com.sunnada.edu.system.pub.common.EXCELObject;
import com.sunnada.edu.system.pub.util.DateUtil;

@Service
@Transactional
public class GroupServiceImpl implements GroupService {
	private static final Logger logger = LoggerFactory
			.getLogger(GroupServiceImpl.class);
	@Resource
	GroupDao groupDao;

	@Override
	public int getGroupCots(HashMap<String, String> params) {
		return groupDao.getGroupCots(params);
	}

	@Override
	public List<?> findGroups(HashMap<String, String> params) {
		return groupDao.findGroups(params);
	}

	@Override
	public String groupDelete(HashMap<String, String> params) {
		String msg = "";
		int num = groupDao.groupDelete(params);
		//删除班组与工人间的关系
		GroupMethods.delRelationFromWorker(params);
		if (num == 0) {
			msg = "删除失败!";
		}
		return msg;
	}

	@Override
	public String groupAdd(HashMap<String, String> params) {
		String msg = "";
		int cot = GroupMethods.getGroupByName(params);
		if (cot > 0) {
			msg = "2";// 班组名称已存在，请更换！
		} else {
			String recId = UUID.randomUUID().toString();//班组ID
			params.put("recId", recId);
			int num = groupDao.groupAdd(params);
			if (num == 0) {
				msg = "0";// 失败
			} else {
				msg = "1";// 成功
				params.put("groupId", recId);
				GroupMethods.workerAdd2Group(params);
				GroupMethods.setMonitor(params);
				GroupMethods.projectAdd2Group(params);				
			}
		}

		return msg;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String groupUpdate(HashMap<String, String> params) {
		String msg = "";
		int cot = GroupMethods.getGroupByName(params);
		 if (cot > 1) {
			msg = "2";// 班组名称已存在，请更换！
		} else {
			int num = groupDao.groupUpdate(params);
			if (num == 0) {
				msg = "0";// 失败
			} else {
				msg = "1";// 成功
				params.put("groupId", params.get("recId"));
				GroupMethods.workerRemoveFromGroup(params);
				GroupMethods.workerAdd2Group(params);
				GroupMethods.setMonitor(params);
				GroupMethods.projectAdd2Group(params);
				
			}
		}
		return msg;
	}

	@Override
	public Map getGroupByRecId(HashMap<String, String> params) {
		return groupDao.getGroupByRecId(params);
	}

	@Override
	public Map<String, String> getGroup(HashMap<String, String> params) {
		return groupDao.getGroup(params);
	}

	@Override
	public String groupEnable(HashMap<String, String> params) {
		String msg = "";
		int num = groupDao.groupEnable(params);
		if (num == 0) {
			msg = "启用失败！";
		}
		return msg;
	}

	@Override
	public String groupDisable(HashMap<String, String> params) {
		String msg = "";
		int num = groupDao.groupDisable(params);
		if (num == 0) {
			msg = "停用失败！";
		}
		return msg;
	}

	@Override
	public void exportGroupWorkers(HashMap<String, String> params,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List dataList = groupDao.exportGroupWorkers(params);
		String recId = UUID.randomUUID().toString();
		String realPath = request.getSession().getServletContext()
				.getRealPath("/file/temp");
		EXCELObject s = new EXCELObject();
		s.seteFilePath(realPath);
		String[] titH = { "用户ID", "姓名", "用户类型", "用户区域" };
		String[] titN = { "Group_ID", "Group_NAME", "Group_TYPE_CHAR",
				"AREA_NAME" };
		s.setFname("系统用户 "); // 临时文件名
		s.setTitle("系统用户"); // 大标题名称
		s.setTitH(titH);
		s.setTitN(titN);
		s.setDataList(dataList);
		File exportFile = null;
		exportFile = s.setData();
		String excelName = "系统用户" + DateUtil.getCurrentTime() + ".xls";
		s.exportExcel("系统用户", excelName, exportFile, request, response);

	}

	public static final String getValueFromCell(Cell cell) {
		if (cell == null) {
			return "";
		}
		cell.setCellType(Cell.CELL_TYPE_STRING);
		String value = null;
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_NUMERIC: // 数字
			value = String.valueOf(cell.getNumericCellValue());
			break;
		case Cell.CELL_TYPE_STRING: // 字符串
			value = cell.getStringCellValue();
			break;
		case Cell.CELL_TYPE_FORMULA: // 公式
			// 用数字方式获取公式结果，根据值判断是否为日期类型
			double numericValue = cell.getNumericCellValue();
			value = String.valueOf(numericValue);
			break;
		case Cell.CELL_TYPE_BLANK: // 空白
			value = StringUtils.EMPTY;
			break;
		case Cell.CELL_TYPE_BOOLEAN: // Boolean
			value = String.valueOf(cell.getBooleanCellValue());
			break;
		case Cell.CELL_TYPE_ERROR: // Error，返回错误码
			value = String.valueOf(cell.getErrorCellValue());
			break;
		default:
			value = StringUtils.EMPTY;
			break;
		}
		return value;
	}

	@Override
	public int getWorkerCots(HashMap<String, String> params) {
		return groupDao.getWorkerCots(params);
	}

	@Override
	public List<?> findWorkers(HashMap<String, String> params) {
		return groupDao.findWorkers(params);
	}

	@Override
	public List<?> getWorkerList(HashMap<String, String> params) {
		return groupDao.getWorkerList(params);
	}

	@Override
	public int getWorkerListCots(HashMap<String, String> params) {
		return groupDao.getWorkerListCots(params);
	}
}
